package com.hykj.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hykj.toolsbean.DB;
import com.hykj.valuebean.aboutus;

public class AboutUsDao extends SuperDao {
	
	private DB mydb= null;
	public AboutUsDao() {
		mydb = new DB();
	}
	public List<aboutus> getAllAboutUs(String strcurrentP, String strcurrentG, String goWhich) throws SQLException {
		String sqlall = "select * from aboutus order by date desc";
		setDaoPage(sqlall, null, strcurrentP, strcurrentG, goWhich);
		int currentP = getDaoPage().getCurrentP();
		int top1 = getDaoPage().getPerR();
		int top2 = (currentP -1) * top1;
		
		String sqlsub = "";
		if (currentP == 1)
			sqlsub = "select * from aboutus order by date desc limit "
					+ top1 + "";
		else
			sqlsub = "select * from aboutus where (date < (select min(date) from (select * from aboutus order by date desc limit "
					+ top2
					+ ") as minv)) order by date desc limit "
					+ top1 + "";

		List<aboutus> alllist = getList(sqlsub, null);
		return alllist;
	}
	
	public aboutus getSingleAboutUs(int id) {
		aboutus single = null;
		String sql = "select * from aboutus where id=?";
		Object[] params = { id };
		try {
			List<?> list = getList(sql, params);
			if (list != null && list.size() != 0)
				single = (aboutus) list.get(0);
			return single;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public void updateAboutUs(aboutus aboutusIns) {
		String sql = "update aboutus set title = ?, date = ?, author = ?, original = ?, article = ?, category = ? where id = ?";
		Object[] params = {aboutusIns.getTitle(), aboutusIns.getDate(), aboutusIns.getAuthor(), aboutusIns.getOriginal(), aboutusIns.getArticle(), aboutusIns.getCategory(), aboutusIns.getId()};
		mydb.doPstm(sql, params);
	}
	
	public void insertAboutUs(aboutus aboutusIns) {
		String sql = "insert aboutus (title, date, author, original, article, category) values (?, ?, ?, ?, ?, ?)";
		Object[] params = {aboutusIns.getTitle(), aboutusIns.getDate(), aboutusIns.getAuthor(), aboutusIns.getOriginal(), aboutusIns.getArticle(), aboutusIns.getCategory()};
		mydb.doPstm(sql, params);
	}
	
	public void deleteAboutUs(int id) {
		String sql = "delete from aboutus where id =?";
		Object[] params = {id};
		mydb.doPstm(sql, params);
	}
	
	public void deleteAboutUs(String url) {
		String sql = "delete from aboutus where id in" + url;
		Object[] params = null;
		mydb.doPstm(sql, params);
	}
	
	public List<aboutus> getCategoryAboutUs(String strcurrentP, String strcurrentG, String goWhich, String category) throws SQLException {
		String sqlall = "select * from aboutus where category = ? order by date desc";
		Object[] params = {category};
		Object[] par = { category, category };
		setDaoPage(sqlall, params, strcurrentP, strcurrentG, goWhich);
		int currentP = getDaoPage().getCurrentP();
		int top1 = getDaoPage().getPerR();
		int top2 = (currentP -1) * top1;
		
		String sqlsub = "";
		if (currentP == 1)
			sqlsub = "select * from aboutus where category = ? order by id desc limit "
					+ top1 + "";
		else
			sqlsub = "select * from aboutus where (id < (select min(id) from (select * from aboutus where category = ? order by id desc limit "
					+ top2
					+ ") as minv)) and category = ? order by id desc limit "
					+ top1 + "";

		if (currentP == 1) {
			List alllist = getList(sqlsub, params);

			return alllist;
		} else {
			List alllist = getList(sqlsub, par);

			return alllist;
		}
	}
	
	private List<aboutus> getList(String sql, Object[] params) throws SQLException {
		List<aboutus> list = null;
		mydb.doPstm(sql, params);
		ResultSet rs = mydb.getRs();
		if (rs != null) {
			list = new ArrayList<aboutus>();
			while (rs.next()) {
				aboutus single = new aboutus();
				single.setId(rs.getInt(1));
				single.setTitle(rs.getString(2));
				single.setDate(rs.getString(3));
				single.setAuthor(rs.getString(4));
				single.setOriginal(rs.getString(5));
				single.setArticle(rs.getString(6));
				single.setCategory(rs.getString(7));

				list.add(single);
			}
			rs.close();
		}
		return list;
	}
	
	
	public void closed() {
		mydb.closed();
	}
	

}
